package roleService

import (
	roleDto "news_for_deepseek_interface/dto/admin/system/role"
	"news_for_deepseek_interface/model"
	adminModel "news_for_deepseek_interface/model/admin"
)

func List(params roleDto.ListRequest) (*roleDto.ListResponse, error) {
	var roles []adminModel.AdminGroupStruct
	var total int64

	db := model.GetInstance().GetDB().Model(&adminModel.AdminGroupStruct{})

	// 构建查询条件
	if params.Name != "" {
		db = db.Where("name LIKE ?", "%"+params.Name+"%")
	}
	if params.Status != nil {
		db = db.Where("status = ?", *params.Status)
	}

	// 获取总数
	if err := db.Count(&total).Error; err != nil {
		return nil, err
	}

	// 分页查询
	offset := (params.Page - 1) * params.Limit
	if err := db.Offset(offset).Limit(params.Limit).Find(&roles).Error; err != nil {
		return nil, err
	}

	// 构建响应
	response := &roleDto.ListResponse{
		Limit: params.Limit,
		Page:  params.Page,
		Total: total,
		List:  make([]roleDto.RoleInfo, 0),
	}

	for _, v := range roles {
		response.List = append(response.List, roleDto.RoleInfo{
			Id:        int(v.Id),
			Name:      v.Name,
			Status:    v.Status,
			CreatedAt: v.CreatedAt.Format("2006-01-02 15:04:05"),
		})
	}

	return response, nil
}
